package com.service.ach.impl;

import com.bean.Ach_record;
import com.bean.City;
import com.bean.Prop_config;
import com.dao.Ach_recordMapper;
import com.dao.CityMapper;
import com.dao.Order_infoMapper;
import com.dao.Prop_configMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.ach.AchService;
import com.service.core.AbstractService;
import org.apache.commons.collections4.MapUtils;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by lvjianqing on 2017/9/19.
 */
@Service
public class AchServiceImpl extends AbstractService<Ach_record, Integer> implements AchService {
    Logger log = Logger.getLogger(AchServiceImpl.class);
    @Autowired
    private Ach_recordMapper ach_recordMapper;
    @Autowired
    private Order_infoMapper order_infoMapper;
    @Autowired
    private Prop_configMapper prop_configMapper;
    @Autowired
    private CityMapper cityMapper;

    @Override
    public Page findAchListByPage(int pageNum, int pageSize, HashMap<String, Object> paraMap) {
        PageHelper.startPage(pageNum, pageSize);
        Page page = (Page) ach_recordMapper.findByParaMap(paraMap);
        return page;
    }

    @Override
    public void countAch() {
        String achTime = new DateTime().plusMonths(-1).toString("yyyy-MM");
        HashMap<String, Object> paraMap = new HashMap<>();
        List<City> citys = cityMapper.findAll(paraMap);
        Set<Integer> citySet = new HashSet<>();
        if (citys != null) {
            for (City c : citys) {
                citySet.add(c.getId());
            }
        }
        List<Map> list = order_infoMapper.findAchByOrderInfo();
        if (list != null) {
            for (Map m : list) {
                Integer cityId = MapUtils.getInteger(m, "cityId");
                if (cityId == null) {
                    continue;
                }
                citySet.remove(cityId);
                Double amounts = MapUtils.getDouble(m, "amounts", 0d);
                Double payAmounts = MapUtils.getDouble(m, "payAmounts", 0d);
                Ach_record ach = new Ach_record();
                ach.setCityid(cityId);
                ach.setOrderamount(BigDecimal.valueOf(amounts));
                ach.setPayamount(BigDecimal.valueOf(payAmounts));
                ach.setAchtime(achTime);
                ach.setStatus((byte) 0);
                ach.setCreatetime(new Date());
                Prop_config prop = prop_configMapper.findByCityid(cityId);
                if (prop != null) {
                    Float agencyprop = prop.getAgencyprop();
                    if (agencyprop != null) {
                        ach.setAgencyprop(agencyprop);
                        double amount = payAmounts.doubleValue() * agencyprop.floatValue() / 100;
                        ach.setAmount(BigDecimal.valueOf(amount));
                    }
                }
                ach_recordMapper.insertSelective(ach);
                log.info("写入：" + ach.toString());
            }
        }
        Iterator<Integer> it = citySet.iterator();
        while (it.hasNext()) {
            Integer cityId = it.next();
            Ach_record ach = new Ach_record();
            ach.setCityid(cityId);
            ach.setOrderamount(BigDecimal.valueOf(0));
            ach.setPayamount(BigDecimal.valueOf(0));
            ach.setAchtime(achTime);
            ach.setStatus((byte) 0);
            ach.setCreatetime(new Date());
            Prop_config prop = prop_configMapper.findByCityid(cityId);
            if (prop != null) {
                Float agencyprop = prop.getAgencyprop();
                if (agencyprop != null) {
                    ach.setAgencyprop(agencyprop);
                    ach.setAmount(BigDecimal.valueOf(0));
                }
            }
            ach_recordMapper.insertSelective(ach);
            log.info("写入：" + ach.toString());
        }
    }

    @Override
    public void closeAch(String achtime, byte status) {
        HashMap<String, Object> paraMap = new HashMap<>();
        paraMap.put("achtime", achtime);
        paraMap.put("status", status);
        ach_recordMapper.updateAllStatusByAchtime(paraMap);
    }
}
